package net.esj.basic.core.engine.util.tree;

import java.io.Serializable;
import java.util.List;

import net.esj.basic.pojo.TreeStructure;
import net.esj.basic.utils.CollectionUtils;

@Deprecated
public abstract class AbstractTreeManager implements TreeProvider {

	private TreeProvider parentManager;
	
	@Override
	public List<TreeStructure> findTree(String type) {
		List<TreeStructure> list = doFindTree(type);
		if(CollectionUtils.isEmpty(list)
				&& parentManager!=null){
			list = parentManager.findTree(type);
		}
		return list;
	}

	@Override
	public List<TreeStructure> loadChildren(TreeStructure parent) {
		List<TreeStructure> list = doLoadChildren(parent);
		if(CollectionUtils.isEmpty(list)
				&& parentManager!=null){
			list = parentManager.loadChildren(parent);
		}
		return list;
	}

	@Override
	public TreeStructure getTree(String type, Serializable id) {
		TreeStructure tree = doGetTree(type, id);
		if(tree==null && parentManager!=null){
			tree = (TreeStructure) parentManager.getTree(type, id);
		}
		return tree;
	}

	protected abstract List<TreeStructure> doFindTree(String type);
	
	protected abstract List<TreeStructure> doLoadChildren(TreeStructure parent);
	
	protected abstract TreeStructure doGetTree(String type, Serializable id);
	
	public void setParentManager(TreeProvider parentManager) {
		this.parentManager = parentManager;
	}

}
